﻿<template>
  <my-container v-loading="pageLoading">
    <!--顶部操作-->
    <template #header>
      <el-form class="ad-form-query" :inline="true" @submit.native.prevent>
        <el-form-item>
          <my-search :fields="fields" @click="onSearch" />
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:carrieroperator:add'])">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:carrieroperator:delete'])">
          <my-confirm-button :disabled="sels.length === 0" :type="'delete'" :placement="'bottom-end'"
            :loading="deleteLoading" style="margin-left: 0px;" @click="onBatchDelete">
            <template #content>
              <p>确定要批量删除吗？</p>
            </template>
            批量删除
          </my-confirm-button>
        </el-form-item>
      </el-form>
    </template>

    <!--列表-->
    <el-table v-loading="listLoading" :data="pageList" highlight-current-row height="'100%'"
      style="width: 100%;height:100%;" @selection-change="onSelsChange">
           
      <el-table-column type="selection" width="50" />
           
      <el-table-column prop="name" label="名称" />
           
      <el-table-column prop="account" label="账户" />
           
      <el-table-column prop="dictionaryName" label="所属类别" />

      <el-table-column prop="isBindPhone" label="绑定手机号">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isBindPhone ? 'success' : 'danger'">{{
          scope.row.isBindPhone ? "绑定" : "不绑定"
          }}</el-tag>
        </template>
      </el-table-column>
           
      <el-table-column prop="shutdownThreshold" label="停机阈值(MB)" width="120" />
           
      <el-table-column prop="isDisable" label="禁用">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDisable ? 'danger' : 'success'">{{
          scope.row.isDisable ? "禁用" : "启用"
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="sort" label="排序" />
           
      <el-table-column prop="creationTime" label="创建时间" :formatter="formatCreatedTime" />
      <el-table-column prop="remark" label="备注" />
           
            <el-table-column
        v-if="checkPermission(['api:admin:carrieroperator:update', 'api:admin:carrieroperator:delete'])" label="操作"
        width="330">
                <template #default="{ $index, row }">
                    <el-button v-if="checkPermission(['api:admin:carrieroperator:update'])"
            @click="onRealNameVerifyEdit($index, row)" type="primary">实名验证</el-button>
                    <el-button v-if="checkPermission(['api:admin:package:update'])" @click="onVerifyAccount(row)"
            type="success">验证账号
          </el-button>
                    <el-button v-if="checkPermission(['api:admin:carrieroperator:update'])"
            @click="onEdit($index, row)">编辑</el-button>
                   
          <my-confirm-button v-if="checkPermission(['api:admin:carrieroperator:delete'])" type="delete"
            :loading="row._loading" @click="onDelete($index, row)" />
                 
        </template>
              </el-table-column>
    </el-table>

    <!--分页-->
    <template #footer>
      <my-pagination ref="pager" :total="total" :checked-count="sels.length" @get-page="pageList" />
    </template>

    <!-- 添加 -->
    <my-window v-if="checkPermission(['api:admin:carrieroperator:add'])" title="添加" :visible.sync="addDialogFormVisible"
      @close="closeAddForm">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="120px" :inline="false">
              <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off" />
        </el-form-item>
              <el-form-item label="账户" prop="account">
          <el-input v-model="addForm.account" auto-complete="off" />
        </el-form-item>
              <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" auto-complete="off" show-password />
        </el-form-item>
              <el-form-item label="密钥" prop="secretKey">
          <el-input v-model="addForm.secretKey" auto-complete="off" show-password />
        </el-form-item>

        <el-form-item label="所属类别" prop="dictionaryId">
          <el-select v-model="addForm.dictionaryId" placeholder="请选择所属类别" style="width: 100%">
            <el-option v-for="item in dictionaryCodeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="绑定手机类别" prop="isBindPhone">
          <el-select v-model="addForm.isBindPhone" placeholder="请选择绑定手机类别" style="width: 100%">
            <el-option v-for="item in bindPhoneDictionaryCodeList" :key="item.value" :label="item.name"
              :value="item.value" />
          </el-select>
        </el-form-item>

              <el-form-item label="排序" prop="sort">
          <el-input-number v-model="addForm.sort"></el-input-number>
        </el-form-item>

              <el-form-item label="停机阈值(MB)" prop="shutdownThreshold">
          <el-input-number v-model="addForm.shutdownThreshold"></el-input-number>
        </el-form-item>

        <el-form-item label="禁用" prop="isDisable">
          <el-switch v-model="addForm.isDisable" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="addForm.remark" maxlength="225" show-word-limit>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="addDialogFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="addFormvalidate" :loading="addLoading" @click="onAddSubmit" />
      </template>
    </my-window>

    <!--编辑窗口-->
    <my-window v-if="checkPermission(['api:admin:carrieroperator:update'])" title="编辑" :visible.sync="editFormVisible"
      @close="closeEditForm">
      <el-form ref="editForm" :model="editForm" :rules="editFormRules" label-width="120px" :inline="false">
              <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off" />
        </el-form-item>
              <el-form-item label="账户" prop="account">
          <el-input v-model="editForm.account" auto-complete="off" />
        </el-form-item>
              <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" auto-complete="off" show-password />
        </el-form-item>
              <el-form-item label="密钥" prop="secretKey">
          <el-input v-model="editForm.secretKey" auto-complete="off" show-password />
        </el-form-item>

        <el-form-item label="所属类别" prop="dictionaryId">
          <el-select v-model="editForm.dictionaryId" placeholder="请选择所属类别" style="width: 100%">
            <el-option v-for="item in dictionaryCodeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="绑定手机类别" prop="isBindPhone">
          <el-select v-model="editForm.isBindPhone" placeholder="请选择绑定手机类别" style="width: 100%">
            <el-option v-for="item in bindPhoneDictionaryCodeList" :key="item.value" :label="item.name"
              :value="item.value" />
          </el-select>
        </el-form-item>

              <el-form-item label="排序" prop="sort">
          <el-input-number v-model="editForm.sort"></el-input-number>
        </el-form-item>
              <el-form-item label="停机阈值(MB)" prop="shutdownThreshold">
          <el-input-number v-model="editForm.shutdownThreshold"></el-input-number>
        </el-form-item>

        <el-form-item label="禁用" prop="isDisable">
          <el-switch v-model="editForm.isDisable" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
             
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="editForm.remark" maxlength="225" show-word-limit>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="editFormvalidate" :loading="editLoading" @click="onEditSubmit" />
      </template>
    </my-window>

    <!--设置实名认证窗口-->
    <my-window v-if="checkPermission(['api:admin:real-name-verify:get'])" title="设置实名认证"
      :visible.sync="realNameVerifyFormVisible" @close="closeRealNameVerifyForm">
      <el-form ref="realNameVerifyForm" :model="realNameVerifyForm" :rules="realNameVerifyFormRules" label-width="120px"
        :inline="false">
        <el-form-item label="实名认证" prop="verifyDictionaryId">
          <el-select v-model="realNameVerifyForm.verifyDictionaryId" placeholder="请选择实名认证" style="width: 100%">
            <el-option v-for="item in verifyDictionaryCodeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="实名显示号码" prop="verifyPhoneDictionaryId">
          <el-select v-model="realNameVerifyForm.verifyPhoneDictionaryId" placeholder="请选择实名显示号码" style="width: 100%">
            <el-option v-for="item in verifyPhoneDictionaryCodeList" :key="item.id" :label="item.name"
              :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="验证地址" prop="verifyUrl">
          <el-input v-model="realNameVerifyForm.verifyUrl" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="验证提示" prop="verifyTip">
          <el-input type="textarea" placeholder="验证提示" v-model="realNameVerifyForm.verifyTip" maxlength="225"
            show-word-limit>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="realNameVerifyFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="realNameVerifyFormvalidate" :loading="realNameVerifyLoading"
          v-if="checkPermission(['api:admin:real-name-verify:update'])" @click="onRealNameVerifySubmit" />
      </template>
    </my-window>

  </my-container>
</template>

<script>
import { formatTime, listToTree, getTreeParents } from '@/utils'
import { getPageList, get, add, update, softDelete, batchSoftDelete, verifyAccount } from '@/api/iot/carrier-operator'
import { getCarrierOperatorRealNameVerify, addOrupdateRealNameVerify } from '@/api/iot/real-name-verify'
import { getCodeList } from "@/api/admin/dictionary-type";
import MyContainer from '@/components/my-container'
import MyConfirmButton from '@/components/my-confirm-button'
import MySearch from '@/components/my-search'
import MySearchWindow from '@/components/my-search-window'
import MyWindow from '@/components/my-window'

export default {
  name: 'carrieroperator',
  components: { MyContainer, MyConfirmButton, MySearch, MySearchWindow, MyWindow },
  data() {
    return {
      // 高级查询字段
      fields: [
        { value: "name", label: "名称", default: true }
      ],
      dynamicFilter: null,
      pageList: [],
      dictionaryCodeList: [],
      bindPhoneDictionaryCodeList: [{ "name": '绑定手机号', value: true }, { "name": "不绑定手机号", value: false }],
      verifyDictionaryCodeList: [],
      verifyPhoneDictionaryCodeList: [],
      total: 0,
      sels: [], // 列表选中列
      listLoading: false,
      pageLoading: false,
      editLoading: false,
      addLoading: false,
      realNameVerifyLoading: false,
      deleteLoading: false,
      // 上传图片等待
      avatarLoading: false,
      // 添加界面
      addDialogFormVisible: false,
      // 实名验证
      realNameVerifyFormVisible: false,
      // 编辑界面是否显示
      editFormVisible: false,
      editFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        account: [{ required: true, message: '请输入账户', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        secretKey: [{ required: true, message: '请输入密钥', trigger: 'blur' }],
        dictionaryId: [{ required: true, message: '请输入所属类别字典Id', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        shutdownThreshold: [{ required: true, message: '请输入停机阈值(MB)', trigger: 'blur' }],
        isDisable: [{ required: true, message: '请输入禁用', trigger: 'blur' }],
      },
      addFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        account: [{ required: true, message: '请输入账户', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        secretKey: [{ required: true, message: '请输入密钥', trigger: 'blur' }],
        dictionaryId: [{ required: true, message: '请输入所属类别字典Id', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        shutdownThreshold: [{ required: true, message: '请输入停机阈值(MB)', trigger: 'blur' }],
        isDisable: [{ required: true, message: '请输入禁用', trigger: 'blur' }],
      },
      realNameVerifyFormRules: {
        carrierOperatorId: [{ required: true, message: '请输入运营商Id', trigger: 'blur' }],
        verifyDictionaryId: [{ required: true, message: '请输入验证方式字典Id', trigger: 'blur' }],
        verifyPhoneDictionaryId: [{ required: true, message: '请输入官方实名跳转复制号码字典Id', trigger: 'blur' }],
        verifyUrl: [{ required: true, message: '请输入验证地址', trigger: 'blur' }],
        verifyTip: [{ required: true, message: '请输入验证提示', trigger: 'blur' }],
      },
      // 编辑界面数据
      editForm: {
        id: "",
        name: "",
        account: "",
        password: "",
        secretKey: "",
        dictionaryId: "",
        isBindPhone: "",
        sort: "",
        shutdownThreshold: "",
        isDisable: false,
        remark: "",
      },
      // 添加界面数据
      addForm: {
        id: "",
        name: "",
        account: "",
        password: "",
        secretKey: "",
        dictionaryId: "",
        isBindPhone: "",
        sort: "",
        shutdownThreshold: "",
        isDisable: false,
        remark: "",
      },
      realNameVerifyForm: {
        id: null,
        carrierOperatorId: "",
        verifyDictionaryId: "",
        verifyPhoneDictionaryId: "",
        verifyUrl: "",
        verifyTip: "",
      }
    }
  },
  async mounted() {
    await this.onGetPageList();
    await this.onGetCodeList();
  },
  methods: {
    formatCreatedTime(row, column, time) {
      return formatTime(time, 'YYYY-MM-DD HH:mm')
    },
    // 查询
    onSearch(dynamicFilter) {
      this.$refs.pager.setPage(1)
      this.dynamicFilter = dynamicFilter
      this.onGetPageList()
    },
    // 显示添加界面
    onAdd() {
      this.addDialogFormVisible = true;
    },
    // 验证账号
    async onVerifyAccount(row) {
      const res = await verifyAccount({ id: row.id });
      if (!res?.success) {
        return;
      }
      this.$message({
        message: res.msg,
        type: "success",
      });
    },
    // 关闭添加
    closeAddForm() {
      this.$refs.addForm.resetFields()
      this.addDialogFormVisible = false
    },
    // 关闭编辑
    closeEditForm() {
      this.$refs.editForm.resetFields()
      this.editFormVisible = false
    },
    // 实名验证编辑
    closeRealNameVerifyForm() {
      this.$refs.realNameVerifyForm.resetFields()
      this.realNameVerifyFormVisible = false
    },
    // 添加验证
    addFormvalidate() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 编辑验证
    editFormvalidate() {
      let isValid = false
      this.$refs.editForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 编辑实名验证验证
    realNameVerifyFormvalidate() {
      let isValid = false
      this.$refs.realNameVerifyForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 删除验证
    deleteValidate(row) {
      let isValid = true
      return isValid
    },
    // 批量删除验证
    batchDeleteValidate() {
      let isValid = true
      return isValid
    },
    // 选择
    onSelsChange(sels) {
      this.sels = sels
    },
    // 获取列表
    async onGetPageList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager,
        dynamicFilter: this.dynamicFilter
      }
      this.listLoading = true
      const res = await getPageList(params)
      this.listLoading = false
      if (!res?.success) {
        return
      }
      this.total = res.data.total
      const data = res.data.list
      data.forEach(d => {
        d._loading = false
      })
      this.pageList = data
    },
    // 根据Code获取字典
    async onGetCodeList() {
      const res = await getCodeList({ code: "CarrierOperatorTypeCode" });
      if (!res?.success) {
        return;
      }
      this.dictionaryCodeList = res.data;

      var realNameVerify = await getCodeList({ code: "RealNameVerifyCode" });
      if (!realNameVerify?.success) {
        return;
      }
      this.verifyDictionaryCodeList = realNameVerify.data;

      var RealNameVerifyPhone = await getCodeList({ code: "RealNameVerifyPhoneCode" });
      if (!RealNameVerifyPhone?.success) {
        return;
      }
      this.verifyPhoneDictionaryCodeList = RealNameVerifyPhone.data;
    },
    // 显示编辑界面
    async onEdit(index, row) {
      this.pageLoading = true
      const res = await get({ id: row.id })
      this.pageLoading = false
      if (res && res.success) {
        const data = res.data;
        this.editForm = data;
        this.editFormVisible = true;
      }
    },
    // 显示实名验证界面
    async onRealNameVerifyEdit(index, row) {
      this.realNameVerifyForm.carrierOperatorId = row.id;
      const res = await getCarrierOperatorRealNameVerify({ id: row.id })
      if (res && res.success) {
        if (res.data) {
          const data = res.data;
          this.realNameVerifyForm = data;
        }
        this.realNameVerifyFormVisible = true;
      }
    },
    // 添加
    async onAddSubmit() {
      this.addLoading = true;
      const para = _.cloneDeep(this.addForm);
      const res = await add(para);
      this.addLoading = false;

      if (!res?.success) {
        return;
      }
      this.$message({
        message: this.$t("admin.addOk"),
        type: "success",
      });
      this.$refs["addForm"].resetFields();
      this.addDialogFormVisible = false;
      this.onGetPageList();
    },
    // 编辑
    async onEditSubmit() {
      this.editLoading = true
      const para = _.cloneDeep(this.editForm)
      const res = await update(para)
      this.editLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      this.$refs['editForm'].resetFields()
      this.editFormVisible = false
      this.onGetPageList()
    },
    // 编辑实名认证
    async onRealNameVerifySubmit() {
      this.realNameVerifyLoading = true
      const para = _.cloneDeep(this.realNameVerifyForm)
      const res = await addOrupdateRealNameVerify(para)
      this.realNameVerifyLoading = false
      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      this.$refs['realNameVerifyForm'].resetFields()
      this.realNameVerifyFormVisible = false
    },
    // 批量删除
    async onBatchDelete() {
      const para = { ids: [] };
      para.ids = this.sels.map((s) => {
        return s.id;
      });
      this.deleteLoading = true;
      const res = await batchSoftDelete(para.ids);
      this.deleteLoading = false;

      if (!res?.success) {
        return;
      }

      this.$message({
        message: this.$t("admin.batchDeleteOk"),
        type: "success",
      });

      this.onGetPageList();
    },
    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = { id: row.id }
      const res = await softDelete(para);
      row._loading = false
      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })
      this.onGetPageList()
    }
  }
}
</script>

<style lang="scss" scoped>
.my-search ::v-deep .el-input-group__prepend {
  background-color: #fff;
}
</style>
